
#include <cstdlib>
#include <multimedia/Fluxo.h>


FluxoDeBytes::FluxoDeBytes(int tamanho)
    : m_pAr((char*) ::malloc(tamanho)),
      m_tamanho(tamanho),
      m_apont(0)
{
    ;
}

FluxoDeBytes::~FluxoDeBytes()
{
    ::free(m_pAr);
}

char FluxoDeBytes::operator[](int i)
{
    return m_pAr[i];
}

char* FluxoDeBytes::c_ptr()
{
    return m_pAr;
}

unsigned int FluxoDeBytes::PegarBytes(int n)
{
    int temp = 0;

    switch(n)    
    {
        case 1:
            temp = m_pAr[m_apont]&0x000000FF;
            break;

        case 2:
            temp = (m_pAr[m_apont+1]&0x000000FF)<<8 | (m_pAr[m_apont]&0x000000FF);
            break;

        case 3:
            temp = (m_pAr[m_apont+2]&0x000000FF)<<16 | (m_pAr[m_apont+1]&0x000000FF)<<8 | (m_pAr[m_apont]&0x000000FF);
            break;

        case 4:
            temp = (m_pAr[m_apont+3]&0x000000FF)<<24 | (m_pAr[m_apont+2]&0x000000FF)<<16 | (m_pAr[m_apont+1]&0x000000FF)<<8 | (m_pAr[m_apont]&0x000000FF);
            break;
    }

    m_apont += n;
    
    return temp;
}

void FluxoDeBytes::Avancar(int d)
{
    m_apont += d;
}

void FluxoDeBytes::Retroceder(int d)
{
    m_apont -= d;
}

